<!--
 * @Description: helloworld
 * @Author: zugelu
 * @Date: 2021-07-27 13:58:29
 * @LastEditors: zugelu
 * @LastEditTime: 2021-07-29 23:07:29
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="test"></div>
</body>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>

<script type="text/babel">
    class Person extends React.Component {
        state = { opacity: 1, timmer: null }

        death = () => {
            ReactDOM.unmountComponentAtNode(document.getElementById('test')); // 组件卸载后，定时器报错
        }

        componentDidMount() {
            this.timmer = setInterval(() => {
                console.log('1');
                let { opacity } = this.state
                opacity -= 0.1
                if (opacity <= 0) opacity = 1
                this.setState({ opacity })
            }, 200)
        }
        componentWillUnmount() {
            clearInterval(this.timmer)
        }

        render() {
            return (
                <div>
                    <h3 style={{ opacity: this.state.opacity }}>这是一段文字，即将被隐藏</h3>
                    <button onClick={this.death}>按钮</button><br />
                </div>
            )
        }
    }

    ReactDOM.render(<Person />, document.querySelector("#test"))
</script>

</html>